package servlet;

import includes.Html;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Html htmlInclude = new Html();
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		RequestDispatcher rd = request.getRequestDispatcher ("main_links.jsp"); 
		HttpSession currentSession = request.getSession();
		
		out.println(htmlInclude.setHeader("Login"));
		out.println("<div id ='header'></div>");
		out.println("<div id='nav-container'>");
		out.println("<div id='top-links'>");
	    rd.include (request, response);
	    
	    out.println("<div id='login'>");
	    String myname =  (String)currentSession.getAttribute("username");
	    
	    if(myname!=null)
	    {
	    	out.println("<p align='right' class='blue'>[ You're logged in as " + myname + " - <a href='Logout'>logout?</a> ]</p>");
	    }
	    else
	    {
	    	out.println("<p class='blue'>[ You're not logged in ]</p>");
	    }
	    out.println("</div>");
	    
	    out.println("</div></div><div id ='content'>");
	    out.println("<div id='content-left'>");
	    out.println("<h3><img src='images/login.png' /> Login</h3><br/>");
	    
	    if(request.getAttribute("login") == null)
	    	request.setAttribute("login", "first");
	    
	    if(myname == null)
	    {
	    	if ( request.getAttribute("login").equals("false") )
	    	{
	    		out.println("<p style='color: red;'>Wrong username or password!</p>");
	    		out.println(this.loginForm(request));
	    	}
	    	else 
	    	{
	    		out.println(this.loginForm(request));
	    	}
	    }
	    else
	    {
	    	if ( request.getAttribute("login").equals("true") )
	    	{
	    		out.println("<p>Welcome old chap!</p><br/>");
	    		out.println("<p><a href='index.jsp'>Frontpage</a></p>");
	    	}
	    	else
	    	{
	    		out.println("<h3>You're already logged in!</h3><br/>");
		    	out.println("<p><a href='Logout'>Logout?</a></p>");
	    	}
	    }
	    out.println("</div></div>");
		out.println(htmlInclude.setFooter());
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	public String loginForm(HttpServletRequest request)
	{
		Cookie[] cookies = request.getCookies();
		String username = "";
		String pw ="";
		for(Cookie c: cookies)
		{
			if(c.getName().equals("Trainstationusername"))
				username = c.getValue();
			if(c.getName().equals("Trainstationpw"))
				pw = c.getValue();
			
		}
		
		
		String form = "<table class='login'>" +
				"<form action='LoginCheckUser' method='post'>" +
				"<tr><td>Username</td><td><input type='text' name='username' value='"+username+"'/></td></tr>" +
				"<tr><td>Password</td><td><input type='password' name='pw' value='"+pw+"'/></td></tr>" +
				"<tr><td>Remember me?</td><td><input type='checkbox' name='remUser'/></td></tr>" +
				"<tr><td>Remember Password?</td><td><input type='checkbox' name='remPw'/></td></tr>" +
				"<tr><td></td><td align='right'><input class='submit' type='submit' value='Login'/></td></tr>" +
				"</form>" +
				"</table>";
		return form;
	}

}
